package online.heycm.platform.nacos;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;

/**
 * Nacos配置推送器
 *
 * @author heycm
 * @since 2023/11/8 19:21
 */
@Slf4j
public class NacosConfPublisherImpl implements NacosConfPublisher {

    private final NacosProperties nacosProperties;

    private final ConfigService configService;

    public NacosConfPublisherImpl(NacosProperties nacosProperties, ConfigService configService) {
        this.nacosProperties = nacosProperties;
        this.configService = configService;
        log.info("Nacos推送器注册配置: {}", nacosProperties);
        log.info("Nacos推送器注册: {}", configService);
    }

    @Override
    public boolean publish(String dataId, String content) {
        return publish(dataId, nacosProperties.getGroup(), content);
    }

    @Override
    public boolean publish(String dataId, String group, String content) {
        try {
            log.info("推送[dataId: {}, group: {}]配置更新: {}", dataId, group, content);
            return configService.publishConfig(dataId, group, content);
        } catch (NacosException e) {
            log.error("推送配置发生异常", e);
        }
        return false;
    }
}
